/*-------------------------------------------------------------------------
 - Copyright (c) 2024-2025 [XD-AMCC TEAM]
 - [XD-AMCC] is licensed under Mulan PSL v2.
 - You can use this software according to the terms and conditions of the Mulan PSL v2.
 - You may obtain a copy of Mulan PSL v2 at:
 -          http://license.coscl.org.cn/MulanPSL2
 - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
 - OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 - TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 - See the Mulan PSL v2 for more details.
 ------------------------------------------------------------------------*/


/**
 * @brief Helper functions for query information of nonTerminals.
 *
 *
 * @author  WXQ#XDU
 * @date    2024.11
 */

#include <stdlib.h>

#include "../include/amcc_error.h"
#include "../include/tree.h"


// information for ALL Non-Terminals of grammar
static t_nonterminal amc_nonterminals [] = {

#define  GRAM_NT_DEF(nt, n1, n2)  { NT_##nt ,  #nt }

    #include "../meta/_gramsymbol.inc"

#undef   GRAM_NT_DEF

};


static int compare_nonterminal (const void * a, const void * b) {
    const t_nonterminal * L = (const t_nonterminal *)a;
    const t_nonterminal * R = (const t_nonterminal *)b;
    return (int)(L->ntId) - (int)(R->ntId);
}


t_nonterminal_ptr nonterm_query_define (EnumNonTerminal id) {
    t_nonterminal key;
    key.ntId = id;
    t_nonterminal_ptr ptrDefine = bsearch( &key, amc_nonterminals,
                    sizeof(amc_nonterminals)/sizeof(amc_nonterminals[0]),
                    sizeof(amc_nonterminals[0]),
                    compare_nonterminal );
    return ptrDefine;
}


const char * nonterm_query_name (EnumNonTerminal id) {
    t_nonterminal_ptr pDef = nonterm_query_define(id);
    if (NULL == pDef) {
        const char * dummy = "anonymous_NonTerminal";
        ERROR_REPORT_BUG("No definition of EnumNonTerminal %d, return name is %s", id, dummy);
        return dummy;
    }

    return pDef->ntName;
}

